/*
 * Copyright (c) 2015, 张涛.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.shaomengjie.smvp.framework.presenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;

import com.example.shaomengjie.smvp.framework.AppStateTracker;
import com.example.shaomengjie.smvp.framework.Trace;
import com.example.shaomengjie.smvp.framework.view.AppDelegate;
import com.example.shaomengjie.smvp.framework.view.IDelegate;
import com.example.shaomengjie.smvp.net.callback.OnResultFailCallback;
import com.example.shaomengjie.smvp.presenter.activity.MainActivity;
import com.example.shaomengjie.smvp.utils.Constant;


/**
 * Presenter base class for Activity
 * Presenter层的实现基类
 *
 * @param <T> View delegate class type
 * @author smj
 */
public abstract class ActivityPresenter<T extends AppDelegate> extends AppCompatActivity implements OnResultFailCallback{
    protected T viewDelegate;
    public final String TAG = this.getClass().getSimpleName();
    public ActivityPresenter() {
        try {
            viewDelegate = getDelegateClass().newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException("create IDelegate error");
        } catch (IllegalAccessException e) {
            throw new RuntimeException("create IDelegate error");
        }
    }

    @Override
    public void onError(int code, String errorMsg) {
        viewDelegate.toast(errorMsg);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppStateTracker.getInstance().setAppState(AppStateTracker.APP_STATE_OFFLINE);
        if (AppStateTracker.getInstance().getAppState() != AppStateTracker.APP_STATE_INIT_OR_KILLED) {
            viewDelegate.create(getLayoutInflater(), null, savedInstanceState);
            setContentView(viewDelegate.getRootView());
            initToolbar();
            viewDelegate.initWidget();
            bindEvenListener();
            initData(savedInstanceState);
        }else {
            protectApp();
        }

    }
    /**
     * 应用被强杀重启app
     */
    protected void protectApp() {
        Trace.d("protectApp " + getClass().getSimpleName() + " killed will do reStart");
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra(Constant.KEY_ACTION, Constant.ACTION_RESTART_APP);
        startActivity(intent);
        overridePendingTransition(0, 0);
        finish();
    }

    protected void initData(Bundle savedInstanceState){};

    protected void bindEvenListener() {}

    protected void initToolbar() {
        Toolbar toolbar = viewDelegate.getToolbar();
        if (toolbar != null) {
            setSupportActionBar(toolbar);
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (viewDelegate == null) {
            try {
                viewDelegate = getDelegateClass().newInstance();
            } catch (InstantiationException e) {
                throw new RuntimeException("create IDelegate error");
            } catch (IllegalAccessException e) {
                throw new RuntimeException("create IDelegate error");
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (viewDelegate.getOptionsMenuId() != 0) {
            getMenuInflater().inflate(viewDelegate.getOptionsMenuId(), menu);
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        viewDelegate = null;
    }

    protected abstract Class<T> getDelegateClass();

   /* @Override
    public boolean onGlobalExceptionHandler(ApiException e) {
        Toast.makeText(this,e.getMessage(),Toast.LENGTH_SHORT).show();
        return true;
    }*/
}
